package com.gengzp.ops.service.impl;

import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.gengzp.common.exception.BusinessException;
import com.gengzp.common.model.database.entity.SysTable;
import com.gengzp.common.model.database.req.AddSysTableReq;
import com.gengzp.common.model.database.req.EditSysTableReq;
import com.gengzp.common.model.database.req.PageSysTableReq;
import com.gengzp.common.model.database.resp.SearchSysTableResp;
import com.gengzp.common.model.pagination.resp.PaginationResult;
import com.gengzp.ops.facade.ISysDatabaseFacadeService;
import com.gengzp.ops.facade.IUserFacadeService;
import com.gengzp.ops.mapper.SysTableMapper;
import com.gengzp.ops.service.ISysTableService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @ClassName SysTableServiceImpl
 * @Description 系统表设计服务层实现类
 * @Author gengzp
 * @Date 2025/7/8 13:40
 */
@Service
public class SysTableServiceImpl implements ISysTableService {

    private static final Logger logger = LoggerFactory.getLogger(SysTableServiceImpl.class);

    @Autowired
    private SysTableMapper sysTableMapper;

    @Autowired
    private ISysDatabaseFacadeService sysDatabaseFacadeService;

    @Autowired
    private IUserFacadeService userFacadeService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSysTable(AddSysTableReq req) {
        if (Objects.isNull(
                sysDatabaseFacadeService.searchSysDatabaseByDatabaseCode(req.getRelationDatabaseCode()))) {
            throw BusinessException.get("当前数据库编码不存在");
        }
        if (CollectionUtils.isNotEmpty(
                sysTableMapper.selectSysTableByCodeOrName(req.getSysTableCode(), req.getSysTableName()))) {
            throw BusinessException.get("当前表已存在, 请勿重复添加");
        }

        SysTable sysTable = new SysTable();
        sysTable.setRelationDatabaseCode(req.getRelationDatabaseCode());
        sysTable.setSysTableCode(req.getSysTableCode());
        sysTable.setSysTableName(req.getSysTableName());
        sysTable.setSysTableDescription(req.getSysTableDescription());
        sysTable.setSysTableDll(req.getSysTableDll());
        sysTableMapper.insertSysTable(sysTable);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editSysTable(EditSysTableReq req) {
        if (Objects.isNull(
                sysDatabaseFacadeService.searchSysDatabaseByDatabaseCode(req.getRelationDatabaseCode()))) {
            throw BusinessException.get("当前数据库编码不存在");
        }
        List<SysTable> sysTables = sysTableMapper.selectSysTableByCodeOrName(req.getSysTableCode(), req.getSysTableCode());
        if (CollectionUtils.isNotEmpty(sysTables) && sysTables.stream().anyMatch(v -> !Objects.equals(v.getId(), Long.valueOf(req.getId())))) {
            throw BusinessException.get("当前表名称或编码已存在, 无法编辑");
        }
        SysTable sysTable = sysTableMapper.selectSysTableById(Long.valueOf(req.getId()));
        if (Objects.isNull(sysTable)) {
            throw BusinessException.get("当前表不存在");
        }

        sysTable.setRelationDatabaseCode(req.getRelationDatabaseCode());
        sysTable.setSysTableCode(req.getSysTableCode());
        sysTable.setSysTableName(req.getSysTableName());
        sysTable.setSysTableDescription(req.getSysTableDescription());
        sysTable.setSysTableDll(req.getSysTableDll());
        sysTableMapper.updateSysTableById(sysTable);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSysTableById(Long tableId) {
        if (Objects.isNull(sysTableMapper.selectSysTableById(tableId))) {
            throw BusinessException.get("当前表已不存在");
        }
        sysTableMapper.deleteSysTableById(tableId);
    }

    @Override
    public SearchSysTableResp searchSysTableById(Long tableId) {
        SysTable sysTable = sysTableMapper.selectSysTableById(tableId);
        if (Objects.isNull(sysTable)) {
            throw BusinessException.get("当前表不存在");
        }

        SearchSysTableResp resp = new SearchSysTableResp();
        resp.setId(String.valueOf(sysTable.getId()));
        resp.setRelationDatabaseCode(sysTable.getRelationDatabaseCode());
        resp.setSysTableCode(sysTable.getSysTableCode());
        resp.setSysTableName(sysTable.getSysTableName());
        resp.setSysTableDescription(sysTable.getSysTableDescription());
        resp.setSysTableDll(sysTable.getSysTableDll());
        resp.setCreatedBy(String.valueOf(sysTable.getCreatedBy()));
        resp.setCreatedTime(sysTable.getCreatedTime());
        resp.setLastModifiedBy(String.valueOf(sysTable.getLastModifiedBy()));
        resp.setLastModifiedTime(sysTable.getLastModifiedTime());
        // 填充操作人信息
        userFacadeService.fillInOperatorInfo(resp);

        return resp;
    }

    @Override
    public PaginationResult<SearchSysTableResp> pageQuerySysTable(PageSysTableReq req) {
        List<SearchSysTableResp> result = Lists.newArrayList();

        List<SysTable> sysTables = sysTableMapper.pageQuerySysTable(req);
        if (CollectionUtils.isEmpty(sysTables)) {
            return PaginationResult.get(result, req);
        }

        // 转换数据类型
        for (SysTable sysTable : sysTables) {
            SearchSysTableResp resp = new SearchSysTableResp();
            resp.setId(String.valueOf(sysTable.getId()));
            resp.setRelationDatabaseCode(sysTable.getRelationDatabaseCode());
            resp.setSysTableCode(sysTable.getSysTableCode());
            resp.setSysTableName(sysTable.getSysTableName());
            resp.setSysTableDescription(sysTable.getSysTableDescription());
            resp.setSysTableDll(sysTable.getSysTableDll());
            resp.setCreatedBy(String.valueOf(sysTable.getCreatedBy()));
            resp.setCreatedTime(sysTable.getCreatedTime());
            resp.setLastModifiedBy(String.valueOf(sysTable.getLastModifiedBy()));
            resp.setLastModifiedTime(sysTable.getLastModifiedTime());
            result.add(resp);
        }
        // 填充操作人信息
        userFacadeService.fillInOperatorInfo(result);

        return PaginationResult.get(result, req);
    }

}
